#ifndef QOS_CPP_JOB_MANAGER
#define QOS_CPP_JOB_MANAGER

#include "job.hpp"
#include "list.hpp"
#include "logger.hpp"
#include <cstdint>
#include <cstring>

namespace qOS {

    /**
     * @brief Job Manager class
     * @details Manages creation, scheduling and execution of multiple Jobs
     */
    class JobManager : private nonCopyable {
    private:
        list jobList;                       /**< Job list */
        size_t maxConcurrentJobs;          /**< Maximum concurrent Job count */
        size_t currentJobCount;            /**< Current Job count */
        
        /**
         * @brief Job monitoring callback function
         * @param e Event parameter
         * @param securityToken Security token
         * @param manager Job manager
         */
        static void monitorCallback(event_t e, uint64_t securityToken, JobManager *manager);

        /**
         * @brief Monitor all Jobs
         */
        void monitorJobs();

        /**
         * @brief Handle completed Job
         * @param job Completed Job
         * @param securityToken Security token for validation
         */
        void handleCompletedJob(Job *job, uint64_t securityToken);

    public:
        /**
         * @brief Constructor
         * @param maxJobs Maximum concurrent Job count
         */
        JobManager(size_t maxJobs = 10);

        /**
         * @brief Destructor
         */
        ~JobManager();

        /**
         * @brief Initialize Job manager
         * @param os Operating system core
         * @return Whether initialization was successful
         */
        bool initialize(core &os);

        /**
         * @brief Add Job
         * @param job Job object
         * @return Whether addition was successful
         */
        bool addJob(Job &job);

        /**
         * @brief Remove Job
         * @param job Job object
         * @return Whether removal was successful
         */
        bool removeJob(Job &job);

        /**
         * @brief Start all Jobs
         * @param os Operating system core
         * @return Whether startup was successful
         */
        bool startAllJobs(core &os);

        /**
         * @brief Add Job with tasks and submit to scheduler
         * @param job Job object
         * @param os Operating system core
         * @param tasks Task array
         * @param count Task count
         * @return Whether addition and submission was successful
         */
        bool addJobAndSubmit(Job &job, core &os, task tasks[], size_t count);

        /**
         * @brief Pause all Jobs
         */
        void pauseAllJobs();

        /**
         * @brief Resume all Jobs
         */
        void resumeAllJobs();

        /**
         * @brief Pause specific Job by name
         * @param name Job name
         * @return Whether pause was successful
         */
        bool pauseJob(const char *name);

        /**
         * @brief Resume specific Job by name
         * @param name Job name
         * @return Whether resume was successful
         */
        bool resumeJob(const char *name);

        /**
         * @brief Force pause all Jobs
         * @return Number of Jobs paused
         */
        size_t forcePauseAllJobs();

        /**
         * @brief Get paused Job count
         * @return Number of paused Jobs
         */
        size_t getPausedJobCount() const;

        /**
         * @brief Get Job count
         * @return Number of currently managed Jobs
         */
        size_t getJobCount() const;

        /**
         * @brief Get running Job count
         * @return Number of running Jobs
         */
        size_t getRunningJobCount() const;

        /**
         * @brief Find Job by name
         * @param name Job name
         * @return Job pointer, returns nullptr if not found
         */
        Job* findJobByName(const char *name);

        /**
         * @brief Clean up resources
         */
        void cleanup();

        /**
         * @brief Perform comprehensive security audit
         * @details Analyzes all jobs for security anomalies and patterns
         */
        void performSecurityAudit() const;

        /**
         * @brief Get Job manager status
         */
        void printStatus();
    };

} // namespace qOS

#endif // QOS_CPP_JOB_MANAGER
